mongodb搭建
——————————————————————————————————————————————————————————————————————————————————————————————————————————————
单例模式
docker run -itd --name mongo -v /docker/mongo/data:/data/db -p 27017:27017 mongo:6.0.2
docker exec -it mongo /bin/bash
#进入mongo
mongosh --port 27017
#切换到admin数据库
use admin
#创建管理员账号
db.createUser(
   {
     user: "admin",
     pwd: "123456",
     roles: [ { role: "root", db: "admin" } ]
   }
);


-------------------------------------------------------------------------
mongodb分片
要求：
前提条件：已经下载完成mongodb镜像文件
搭建方案：
路由服务器 1台
配置服务器 1
分片服务器 3个副本集
docker v23+
1、Config Server：配置服务器存储集群的元数据和配置设置。
2、Shard：数据分片，每个分片包含分片数据的一个子集。 每个分片都可以部署为一个副本集。
3、Mongos：mongos 充当查询路由器，提供客户端应用程序和分片集群之间的接口。 从 MongoDB 4.4 开始，mongos 可以支持对冲读取以最小化延迟。
https://blog.csdn.net/one2threexm/article/details/117822683
搭建步骤
一、创建keyFIle
 sudo mkdir /docker/mongo
 sudo chmod 777 /docker/mongo
 sudo openssl rand -base64 128 > /docker/mongo/keyFile
 sudo chmod 600 /docker/mongo/keyFile
二、搭建Config Server
mkdir -p /docker/mongo/mongo-config-0/db
mkdir -p /docker/mongo/mongo-config-0/configdb
chmod -R 777 /docker/mongo/mongo-config-0/db
chmod -R 777 /docker/mongo/mongo-config-0/configdb
1）初始化config server配置文件
    sudo cp /docker/mongo/keyFile /docker/mongo/mongo-config-0/configdb/keyFile
    sudo chmod 600 /docker/mongo/mongo-config-0/configdb/keyFile

    cat > /docker/mongo/mongo-config-0/configdb/mongod.conf <<EOF
    net:
      port: 21000
      bindIpAll: true
    security:
      keyFile: /data/configdb/keyFile
      authorization: enabled
    replication:
       replSetName: rs-conf
    sharding:
       clusterRole: configsvr
       archiveMovedChunks: false
    storage:
      wiredTiger:
        engineConfig:
          cacheSizeGB: 0.5
    EOF

2）查看docker网络名称docker network ls
    NETWORK ID     NAME                   DRIVER    SCOPE
    5ae478587ab5   bridge                 bridge    local
    874a412cb80a   host                   host      local
    9d2c91277cb9   none                   null      local
    b4c1e5f81207   rediscluster_default   bridge    local

3）启动config server
    docker run -d \
    --network bridge \
    --restart=always \
    -v /docker/mongo/mongo-config-0/db:/data/db \
    -v /docker/mongo/mongo-config-0/configdb:/data/configdb \
    -v /etc/localtime:/etc/localtime:ro \
    --name mongo-config-0 mongo:6 mongod \
    -f /data/configdb/mongod.conf

4）初始化config server的复制集

    docker exec -it mongo6-config-0 /bin/bash    #进入容器
    mongosh --port 21000   #进入mongo
    use admin   #切换到admin数据库
    db.auth('admin', '123456')    #登录管理员账号
    #初始confi server复制集配置
    rs.initiate(
      {
        _id: "rs-conf",
        members: [
          { _id : 0, host : "192.168.2.127:21000"}
        ]
      }
    )
    #创建管理员账号
    db.createUser(
       {
         user: "admin",
         pwd: "123456",
         roles: [ { role: "root", db: "admin" } ]
       }
三、搭建Shard-0
1）创建Shard-0数据目录

mkdir -p /app/mongo6-shard-0/db
mkdir -p /app/mongo6-shard-0/configdb
chmod -R 777 /app/mongo6-shard-0/db
chmod -R 777 /app/mongo6-shard-0/configdb
 2）初始化Shard-0配置文件
cat > /app/mongo6-shard-0/configdb/mongod.conf <<EOF
net:
  port: 22000
  bindIpAll: true
security:
  keyFile: /data/configdb/keyFile
  authorization: enabled
replication:
   replSetName: rs-shard-0
sharding:
   clusterRole: shardsvr
   archiveMovedChunks: false
storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
EOF
sudo cp /app/keyFile /app/mongo6-shard-0/configdb/keyFile
sudo chmod 600 /app/mongo6-shard-0/configdb/keyFile
3）启动shard-0

docker run -d \
--network host \
--restart=always \
-v /app/mongo6-shard-0/db:/data/db \
-v /app/mongo6-shard-0/configdb:/data/configdb \
-v /etc/localtime:/etc/localtime:ro \
--name mongo6-shard-0 mongo:6 mongod \
-f /data/configdb/mongod.conf
4）初始化shard-0复制集

#进入容器
docker exec -it mongo6-shard-0 /bin/bash

#进入mongo
mongosh --port 22000

#切换到admin数据库
use admin


#初始confi server复制集配置
rs.initiate(
  {
    _id: "rs-shard-0",
    members: [
      { _id : 0, host : "192.168.2.127:22000"}
    ]
  }
)


#创建管理员账号
db.createUser(
   {
     user: "admin",
     pwd: "123456",
     roles: [ { role: "root", db: "admin" } ]
   }
);


#登录管理员账号
db.auth('admin', '123456')
4、搭建Shard-1
1）创建shard-1数据目录

mkdir -p /app/mongo6-shard-1/db
mkdir -p /app/mongo6-shard-1/configdb
chmod -R 777 /app/mongo6-shard-1/db
chmod -R 777 /app/mongo6-shard-1/configdb
 2）初始化shard-1配置文件

cat > /app/mongo6-shard-1/configdb/mongod.conf <<EOF
net:
  port: 23000
  bindIpAll: true

security:
  keyFile: /data/configdb/keyFile
  authorization: enabled

replication:
   replSetName: rs-shard-1

sharding:
   clusterRole: shardsvr
   archiveMovedChunks: false

storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
EOF
sudo cp /app/keyFile /app/mongo6-shard-1/configdb/keyFile
sudo chmod 600 /app/mongo6-shard-1/configdb/keyFile
  3）启动shard-1

docker run -d \
--network host \
--restart=always \
-v /app/mongo6-shard-1/db:/data/db \
-v /app/mongo6-shard-1/configdb:/data/configdb \
-v /etc/localtime:/etc/localtime:ro \
--name mongo6-shard-1 mongo:6 mongod \
-f /data/configdb/mongod.conf
4）初始化shard-1复制集

#进入容器
docker exec -it mongo6-shard-1 /bin/bash

#进入mongo
mongosh --port 23000

#切换到admin数据库
use admin

#创建管理员账号
db.createUser(
   {
     user: "admin",
     pwd: "123456",
     roles: [ { role: "root", db: "admin" } ]
   }
);

#登录管理员账号
db.auth('admin', '123456')

#初始confi server复制集配置
rs.initiate(
  {
    _id: "rs-shard-1",
    members: [
      { _id : 0, host : "192.168.2.127:23000"}
    ]
  }
)
 5）搭建Shard-2
1、创建shard-2数据目录

mkdir -p /app/mongo6-shard-2/db
mkdir -p /app/mongo6-shard-2/configdb
chmod -R 777 /app/mongo6-shard-2/db
chmod -R 777 /app/mongo6-shard-2/configdb
 2、初始化shard-2配置文件

cat > /app/mongo6-shard-2/configdb/mongod.conf <<EOF
net:
  port: 24000
  bindIpAll: true

security:
  keyFile: /data/configdb/keyFile
  authorization: enabled

replication:
   replSetName: rs-shard-2

sharding:
   clusterRole: shardsvr
   archiveMovedChunks: false

storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
EOF

sudo cp /app/keyFile /app/mongo6-shard-2/configdb/keyFile
sudo chmod 600 /app/mongo6-shard-2/configdb/keyFile
  3、启动shard-2

docker run -d \
--network host \
--restart=always \
-v /app/mongo6-shard-2/db:/data/db \
-v /app/mongo6-shard-2/configdb:/data/configdb \
-v /etc/localtime:/etc/localtime:ro \
--name mongo6-shard-2 mongo:6 mongod \
-f /data/configdb/mongod.conf
4、初始化shard-2复制集

#进入容器
docker exec -it mongo6-shard-2 /bin/bash

#进入mongo
mongosh --port 24000

#切换到admin数据库
use admin

#创建管理员账号
db.createUser(
   {
     user: "admin",
     pwd: "123456",
     roles: [ { role: "root", db: "admin" } ]
   }
);

#登录管理员账号
db.auth('admin', '123456')

#初始confi server复制集配置
rs.initiate(
  {
    _id: "rs-shard-2",
    members: [
      { _id : 0, host : "192.168.2.127:24000"} ,   #可以配置n个
      {_id:1,host:" 172.17.0.9:27017"},
      ...
      {_id:2,host:" 172.17.0.10:27017"}
    ]
  }
)
六、搭建Mongos-0
1、创建mongos-0数据目录

mkdir -p /app/mongos6-0/db
mkdir -p /app/mongos6-0/configdb
chmod -R 777 /app/mongos6-0/db
chmod -R 777 /app/mongos6-0/configdb
 2、初始化mongos-0配置文件

cat > /app/mongos6-0/configdb/mongos.conf <<EOF
net:
  port: 27017
  bindIpAll: true

security:
  keyFile: /data/configdb/keyFile


sharding:
   configDB: rs-conf/192.168.2.127:21000

EOF
cp /app/keyFile /app/mongos6-0/configdb/keyFile

#如果启动mongos报bad file，执行
chown 999 /app/mongos6-0/configdb/keyFile
 3、启动mongos-0

docker run -d \
--network host \
--restart=always \
-v /app/mongos6-0/db:/data/db \
-v /app/mongos6-0/configdb:/data/configdb \
-v /etc/localtime:/etc/localtime:ro \
--name mongos6-0 mongo:6 mongos \
-f /data/configdb/mongos.conf
4、登录mongos-0，进行分片配置

#进入容器
docker exec -it mongos6-0 /bin/bash

#进入mongo
mongosh --port 27017

#切换到admin数据库
use admin

#登录
db.auth('admin','123456')

#添加分片
sh.addShard("rs-shard-0/192.168.2.127:22000");
sh.addShard("rs-shard-1/192.168.2.127:23000");
sh.addShard("rs-shard-2/192.168.2.127:24000");

#对指定库进行分片
sh.enableSharding("testdb")

# 使用hash分片键
sh.shardCollection('testdb.collection', {'field':'hashed'})
5、分片相关命令，在mongos上使用

# 启用数据库分片:
sh.enableSharding("<database>")

# 使用hash分片键
sh.shardCollection('db.collection', {'field':'hashed'})

# 使用递增分片键
sh.shardCollection('db.collection', { field: 1})

# 查看分片是否成功
db.collection.stats().sharded

# 查看数据分布
db.collection.getShardDistribution()













shardMaster1 172.17.0.8
shardSlavel1 172.17.0.9
shardSlavel2 172.17.0.10
mongo6-config-0  172.17.0.5
（3）进入容器，设置主从副本集
进入容器：docker exec -it shardMaster1 mongo
初始化及设置副本集

/*这里的...代表空格，不要删除，复制后改集群名称、ip、端口号*/
rs.initiate(
... {
...     _id:"rsconfig",
...     members:[
...        {_id:0,host:" 172.17.0.8:27017"},
...        {_id:1,host:" 172.17.0.9:27017"},
...        {_id:2,host:" 172.17.0.10:27017"}
...     ]
... }
... )